package com.ice.netty.modbus.handle;

import com.ice.netty.modbus.config.MiiMessage;
import com.ice.netty.modbus.data.*;
import com.ice.netty.modbus.server.NettyTestServer;
import com.ice.remoting.common.CommonConstant;
import com.ice.remoting.common.RemotingHelper;
import com.ice.remoting.data.RemotingCommand;
import com.ice.remoting.netty.NettyRequestProcessor;

import io.netty.channel.ChannelHandlerContext;
import io.netty.util.AttributeKey;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class DefaultProcess implements NettyRequestProcessor {

    private NettyTestServer nettyTestServer;

    public DefaultProcess(NettyTestServer nettyTestServer) {
        this.nettyTestServer = nettyTestServer;
    }

    @Override
    public RemotingCommand processRequest(ChannelHandlerContext ctx, RemotingCommand cmd) {
        MsgBaseData msgBaseData = this.nettyTestServer.getDataFactory().buildData(cmd.getCommand(), cmd.getBody());
        RemotingCommand response = null;
        switch (cmd.getCommand()) {
            case MiiMessage.HEART_COMMAND:
                handleHeartBeant(ctx, msgBaseData);
                break;
            case MiiMessage.DEVICE_NO_COMMAND:
                response = handleDevice(ctx, msgBaseData);
                break;
            case MiiMessage.BACK_LIGHT_COMMAND:
                handleBackLight(ctx, msgBaseData);
                break;
            case MiiMessage.CARD_COMMAND:
                response = handleSwipeCard(ctx, msgBaseData);
                break;
            case MiiMessage.SCAN_CODE_COMMAND:
                response = handleScanCode(ctx, msgBaseData);
                break;
            default:
                log.info("不知道命令：{}", cmd.getCommand());
        }
        return response;
    }

    /**
     * 处理心跳
     * 
     * @Author wangwei
     * @Date 2024/1/22
     */
    private RemotingCommand handleHeartBeant(ChannelHandlerContext ctx, MsgBaseData msgBaseData) {
        final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(ctx.channel());
        nettyTestServer.getDeviceManager().updateBrokerInfoUpdateTimestamp(remoteAddress);
        return null;
    }

    private RemotingCommand handleBackLight(ChannelHandlerContext ctx, MsgBaseData msgBaseData) {
        BackLightData backLightData = (BackLightData)msgBaseData;
        log.info("背光灯状态码：: {},是否亮灯：{}", backLightData.getStatusCode(), backLightData.getStatusCode() == 0 ? "亮" : "不良");
        return null;
    }

    private RemotingCommand handleSwipeCard(ChannelHandlerContext ctx, MsgBaseData msgBaseData) {
        SwipeCardData swipeCard = (SwipeCardData)msgBaseData;
        log.info("刷卡，卡号信息： " + swipeCard.getCardNo());
        return null;
    }

    private RemotingCommand handleDevice(ChannelHandlerContext cxt, MsgBaseData msgBaseData) {
        DeviceInfoData deviceInfoData = (DeviceInfoData)msgBaseData;
        log.info("设备编码：{}", deviceInfoData.getDeviceNo());
        // 设置唯一编号
        this.nettyTestServer.setCustomerUniqueIdentity(cxt.channel(), deviceInfoData.getDeviceNo());
        // 设置设备编号
        cxt.channel().attr(AttributeKey.valueOf(CommonConstant.DEVICE_NO)).setIfAbsent(deviceInfoData.getDeviceNo());
        return null;
    }

    private RemotingCommand handleScanCode(ChannelHandlerContext cxt, MsgBaseData msg) {
        ScanCodeData scanCodeData = (ScanCodeData)msg;
        log.info("开锁状态：{}", scanCodeData.getStatusCode());
        return null;
    }
}
